<?php

namespace app\api\model;

use think\Model;


class ShopPayOrder extends Model
{

    

    

    // 表名
    protected $table = 'tp_shop_pay_order';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'status_text',
        'finish_time_text',
        'create_time_text',
        'pay_time_text',
        'shipping_time_text',
        'kuaidi_time_text',
        'benefit_type_text',
        'is_account_text',
        'is_award_text'
    ];
    

    
    public function getStatusList()
    {
        return [0 => __('待支付'),1 => __('待使用'),2 => __('已取消'),3 => __('已完成'),4 => __('已发货')];
    }

    public function getBenefitTypeList()
    {
        return ['1' => __('Benefit_type 1'), '2' => __('Benefit_type 2')];
    }

    public function getIsAccountList()
    {
        return ['0' => __('Is_account 0'), '1' => __('Is_account 1')];
    }

    public function getIsAwardList()
    {
        return ['0' => __('Is_award 0'), '1' => __('Is_award 1')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ?: ($data['status'] ?? '');
        $list = $this->getStatusList();
        return $list[$value] ?? '';
    }


    public function getFinishTimeTextAttr($value, $data)
    {
        $value = $value ?: ($data['finish_time'] ?? '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ?: ($data['create_time'] ?? '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getPayTimeTextAttr($value, $data)
    {
        $value = $value ?: ($data['pay_time'] ?? '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getShippingTimeTextAttr($value, $data)
    {
        $value = $value ?: ($data['shipping_time'] ?? '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getKuaidiTimeTextAttr($value, $data)
    {
        $value = $value ?: ($data['kuaidi_time'] ?? '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getBenefitTypeTextAttr($value, $data)
    {
        $value = $value ?: ($data['benefit_type'] ?? '');
        $list = $this->getBenefitTypeList();
        return $list[$value] ?? '';
    }


    public function getIsAccountTextAttr($value, $data)
    {
        $value = $value ?: ($data['is_account'] ?? '');
        $list = $this->getIsAccountList();
        return $list[$value] ?? '';
    }


    public function getIsAwardTextAttr($value, $data)
    {
        $value = $value ?: ($data['is_award'] ?? '');
        $list = $this->getIsAwardList();
        return $list[$value] ?? '';
    }

    protected function setFinishTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setCreateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setPayTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setShippingTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setKuaidiTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    public function shop(){
        return $this->hasOne("UserStore",'user_id','shop_user_id')->field('id,user_id,store_name,address');
    }
}
